package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author ：wm
 * @date ：Created in 2022年06月07日
 * @description ：
 * @version: 1.0
 */
@Slf4j
@RestController
//预约设置的公共浏览器地址为ordersetting
@RequestMapping("ordersetting")
//通过OrderSettingController实现预约设置功能
public class OrderSettingController {
    //把service层交给springboot管理
    @Autowired
    private OrderSettingService orderSettingService;

    //实现预约设置日期的显示
    //通过get方法接收getOrderSettingByMonth请求
    @GetMapping("getOrderSettingByMonth")
    //返回值是Result类型,通过与前端传输的名字一样的名字(year 年 month 月 接收前端发过来的数据)
    public Result getOrderSettingByMonth(Integer year, Integer month) {
        //测试是否接到前端传输的值
        log.info("预约设置年为：{},月为{}", year, month);
        //调用service接口实现日期显示功能,传输数据是year,month
        List<Map> order = orderSettingService.getOrderDay(year, month);
        //对前端进行值的返回,标记为true,提示信息为预约设置成功
        return new Result(true, MessageConst.GET_ORDERSETTING_SUCCESS, order);
    }

    //通过post方式接收editNumberByDate请求
    //实现可预约人数的编辑
    @PostMapping("editNumberByDate")
    //通过RequestBody方法接收前端post方法传输过来的数据
    //主要功能是实现可预约人数的编辑
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting) {
        //调用service方法实现对可预约人数的编辑功能,传输的是实体类orderSetting
        orderSettingService.editNumberByDate(orderSetting);
        //上面调用方法没有返回值,状态标记符为true,提示编辑成功
        return new Result(true, MessageConst.ORDERSETTING_SUCCESS);
    }

    @PostMapping("/upload")
    public Result upload(MultipartFile excelFile) {
        String fileName = excelFile.getOriginalFilename();


        //创建读取excel的对象
        Workbook workbook = null;

        try {
            if (fileName.endsWith(".xlsx")) {
                workbook = new XSSFWorkbook(excelFile.getInputStream());
            } else if (fileName.endsWith(".xls")) {
                workbook = new HSSFWorkbook(excelFile.getInputStream());
            } else {
                return new Result(false, "文件格式不正确，请检查重试");
            }

        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, "文件上传失败");
        }
        Sheet sheet = workbook.getSheetAt(0);
        //遍历所有行
        for (int i = 1; i <= sheet.getLastRowNum(); i++) {
            //获取每一行
            Row row = sheet.getRow(i);
            OrderSetting orderSetting = new OrderSetting();

            //保存日期
            orderSetting.setOrderDate(row.getCell(0).getDateCellValue());
            //保存人数
            orderSetting.setNumber((int)row.getCell(1).getNumericCellValue());
            //执行编辑方法，如果日期不存在会新增，已存在做更新
            orderSettingService.editNumberByDate(orderSetting);
        }

        return new Result(true, MessageConst.IMPORT_ORDERSETTING_SUCCESS);
    }
}
